from django.db import models
from users.models import User
from enum import Enum, auto


# 定义知识图谱状态字典数据
class KnowledgeGraphStatus(Enum):
    PENDING = ('pending', '待构建')  # 待构建
    BUILDING = ('building', '构建中')  # 构建中
    COMPLETED = ('completed', '构建完成')  # 构建完成
    FAILED = ('failed', '构建失败')  # 构建失败
    DELETED = ('deleted', '已删除')  # 已删除 = ('failed', '构建失败')  # 构建失败

    @property
    def value(self):
        return self._value_[0]

    @property
    def label(self):
        return self._value_[1]


# 定义知识图谱类型字典数据
class KnowledgeGraphType(Enum):
    STRUCTURED = ('structured', '结构化')  # 结构化
    UNSTRUCTURED = ('unstructured', '非结构化')  # 非结构化

    @property
    def value(self):
        return self._value_[0]

    @property
    def label(self):
        return self._value_[1]


class KnowledgeGraphDBSourceType(Enum):
    MYSQL = ('mysql', 'MySQL')
    POSTGRESQL = ('postgresql', 'PostgreSQL')
    ORACLE = ('oracle', 'Oracle')
    SQLSERVER = ('sqlserver', 'SQL Server')

    @property
    def value(self):
        return self._value_[0]

    @property
    def label(self):
        return self._value_[1]


# 知识图谱模型
class KnowledgeGraph(models.Model):
    name = models.CharField('图谱名称', max_length=100)
    graph_type = models.CharField(
        '构建方式',
        max_length=20,
        choices=[(tag.value, tag.label) for tag in KnowledgeGraphType]
    )
    build_total = models.IntegerField('构建数量', default=0)
    already_build = models.IntegerField('已经构建数量', default=0)
    created_at = models.DateTimeField('构建时间', auto_now_add=True)
    created_by = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='创建者')
    status = models.CharField(
        '构建状态',
        max_length=20,
        choices=[(status.value, status.label) for status in KnowledgeGraphStatus],
        default=KnowledgeGraphStatus.PENDING.value
    )

    class Meta:
        db_table = 'knowledge_graphs'
        verbose_name = '知识图谱'
        verbose_name_plural = verbose_name
        ordering = ['-created_at']


class GraphDataSource(models.Model):
    """
    图谱数据源配置
    用于存储结构化数据源的连接信息
    """
    DB_TYPE_CHOICES = [
        ('mysql', 'MySQL'),
        ('postgresql', 'PostgreSQL'),
        ('oracle', 'Oracle'),
        ('sqlserver', 'SQL Server'),
    ]

    graph = models.OneToOneField(KnowledgeGraph, on_delete=models.CASCADE, related_name='data_source')
    db_type = models.CharField(
        '数据库类型',
        max_length=20,
        choices=[(dtype.value, dtype.label) for dtype in KnowledgeGraphDBSourceType],
    )
    db_name = models.CharField('数据库名称', max_length=100)
    username = models.CharField('用户名', max_length=100)
    password = models.CharField('密码', max_length=100)
    host = models.CharField('主机地址', max_length=200)
    port = models.IntegerField('端口号')

    class Meta:
        db_table = 'graph_data_sources'
        verbose_name = '图谱数据源'
        verbose_name_plural = verbose_name


class GraphDataSourceTable(models.Model):
    """
    图谱构建需要的数据表
    用于存储结构化数据源的连接信息
    """
    graph = models.ForeignKey(KnowledgeGraph, on_delete=models.CASCADE, related_name='data_source_table')
    table_name = models.CharField('数据表名称', max_length=100)
    commit = models.CharField('数据表备注', max_length=100)

    class Meta:
        db_table = 'graph_data_source_tables'
        verbose_name = '图谱数据构建表'
        verbose_name_plural = verbose_name
        unique_together = ('graph', 'table_name')


class UnstructuredFile(models.Model):
    """
    非结构化文件
    用于存储上传的文件信息
    """
    graph = models.ForeignKey(KnowledgeGraph, on_delete=models.CASCADE, related_name='files')
    file_name = models.CharField('文件名', max_length=200)
    file_path = models.CharField('文件路径', max_length=500)
    file_type = models.CharField('文件类型', max_length=120)
    uploaded_at = models.DateTimeField('上传时间', auto_now_add=True)

    class Meta:
        db_table = 'unstructured_files'
        verbose_name = '非结构化文件'
        verbose_name_plural = verbose_name
